package com.mad.base.manager;

import java.util.ArrayList;

import com.mad.base.gui.EWndType;
import com.mad.base.objects.GameObject;
import com.mad.base.objects.grid.EChangeableFieldAttribute;
import com.mad.base.objects.grid.IChangeableFieldAttributes;
import com.mad.base.objects.grid.FieldCoverer;
import com.mad.base.objects.player.EPlayerType;
import com.mad.base.objects.player.Player;
import com.mad.base.objects.ships.EShipType;
import com.mad.base.system.EDirection;
import com.mad.base.system.ID;

public class PlayerManager
    extends SubManager
{

    private GridManager gridManager; 
    private GuiManager guiManager; 
    private ShipManager shipManager; 
    private final EPlayerType playerType ;  
    private Player player ; 
    
    public GridManager getGridManager() {
        return gridManager;
    }

    public GuiManager getGuiManager() {
        return guiManager;
    }

    public ShipManager getShipManager()
    {
        return shipManager;
    }

    public EPlayerType getPlayerType()
    {
        return playerType;
    }

    public EWndType getWndType(){
        return this.playerType.getWndType(); 
    }
    
    public Player getPlayer()
    {
        return player;
    }
    
    public PlayerManager(Manager parentManager, EPlayerType pType)
    {
        super( parentManager );
        this.playerType = pType ; 
    }

    public ArrayList<ID> getFieldsByAttributes(EChangeableFieldAttribute fieldAttr){
        return this.gridManager.getFieldsByAttributes( fieldAttr ); 
    }
    
    public void spawnNewShip(EShipType shipType, ID bowID, EDirection dir){
        if(this.gridManager.canAddShipToField( bowID, dir, shipType.getLength() )){
            this.gridManager.addShipToField( bowID, dir, shipType.getLength() );
            this.shipManager.spawnShip( shipType, bowID, dir ); 
            this.update( this.getFieldsByAttributes( EChangeableFieldAttribute.COVERED ) ); 
            System.out.println(this.playerType+" spawned new "+shipType.getName()+" on field "+bowID+" in "+dir+" direction");
        }else
            System.out.println(this.getPlayerType()+" cannot spawn "+shipType.getName()+" on field "+bowID+" in "+dir+" direction");
    }
    
    @Override
    public void init( Object o )
    {
        this.player = new Player(this.playerType);    
        this.gridManager = new GridManager(this); 
        this.shipManager = new ShipManager(this); 
        this.guiManager = new GuiManager(this); 
        this.gridManager.init( o ); 
        this.shipManager.init( o ); 
        this.guiManager.init( o ); 
    }

    @Override
    public void exit( Object o )
    {
        this.guiManager.exit(o); 
        this.gridManager.exit(o); 
        this.shipManager.exit(o); 
    }

    @Override
    public void update( Object o )
    {
        this.gridManager.update( o ); 
        this.shipManager.update( o ); 
        this.guiManager.update( o ); 
    }

}
